/**
 * \file  antiviral_selector_inline.h
 * \brief Inline methods definitions of Antiviral Selector class.
 *
 * LAV: Lib Anti Virals
 * <http://lav.googlecode.com/>
 * Copyright (C) 2013 Lenarduzzi Federico, Boretto Martin, Alberione Fernando, Ocampo Emanuel.
 *
 * This file is part of the LAV project.
 *
 * Contents:   Inline methods definitions of Antiviral Selector class.
 *
 * System:        LAV
 * Language:      C++
 *
 * \authors Boretto Martin, Lenarduzzi Federico, Ocampo Emanuel, Alberione Fernando.
 *
 * LAV is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LAV is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ASO.  If not, see <http://www.gnu.org/licenses/>.
 *
 */




#ifndef ANTIVIRAL_SELECTOR_INLINE_H
#error Internal header file, DO NOT include this.
#endif

#include <assert.h>

namespace lav 
{

/**\fn factorial(size_t n) 
 * \brief Function to calculate Factorial
 * 
 * This function calculate factorial(n), but n must be less or equal than a FACTORIAL_MAX_VALUE
 * 
 * @param n unsigned int to calculate factorial.
 * @return the result to calculate factorial to n
 */
template <class T>
inline T AntiviralSelector::factorial(size_t n)
{
    assert(n <= FACTORIAL_MAX_VALUE);
   	static const T value[] = { T(1),    /*!< fact(0) = T(1) */ 
                               T(1),    /*!< fact(1) = T(1) */ 
                               T(2),    /*!< fact(2) = T(2) */ 
                               T(6) };  /*!< fact(3) = T(6) */ 
   	return value[n];
}


/**
 * This method select antiviral that can be applied to the sequence.
 *
 *@Param antiviral_set This set contains all antivirals.
 *@Param sequence Represents the patient's virus
 *@Param applicable_avs Contains the antivirals that applied to the sequence.
 *
 */
inline void AntiviralSelector::select_antivirals(const AntiviralSet& antiviral_set, const biopp::PseudoNucSequence& sequence, AntiviralSet& applicable_avs)
{
    /*!< iteration over all antivirals */
    for (AVIterator it = antiviral_set.begin_AVIterator(); !it.end(); ++it)
    {
        /*!< distance between the current antiviral and sequence */
        const biopp::NucDistance distance = (*it)->applies(sequence);
        if (distance)
            applicable_avs.add_antiviral(**it);
    }
}


/**
 * This method calculates the virus probabilities to escape from some antiviral
 *
 *@Param antiviral_set Represents antivirals which will be ranked
 *@Param sequence Represents the patient's virus
 *@Param rank Represents the ranker result
 *
 */
inline void AntiviralSelector::rank_antivirals(const AntiviralSet& antiviral_set, const biopp::PseudoNucSequence& sequence, RankAvProb& rank)
{
    for (AVIterator it_av = antiviral_set.begin_AVIterator(); !it_av.end(); ++it_av)
    {
        /*!< iteration on antivirals_set */
        double p = 0.0;
        if ((*it_av)->applies(sequence) > 0)
        {
            ResisBasedAntiviral::ResistancePositionContainer resistances;
            (*it_av)->get_resistances(resistances);
            for (ResisBasedAntiviral::ResistancePositionContainer::const_iterator it_res = resistances.begin(); it_res != resistances.end(); ++it_res)
            {
                /*!< iteration on resistances of the antiviral */
                process_resistances(it_res, sequence, p);
            }
        }
        AvProbPair pair(**it_av, p);
        rank.insert(pair);
    }
}


/**
 * This method calculates the virus probabilities to escape from some antiviral
 *
 *@Param it_res Pointer to resistances list of the current antiviral.
 *@Param sequence Represents the patient's virus
 *@Param p Represents probability value of the current antiviral.
 *
 */
inline void AntiviralSelector::process_resistances(const ResisBasedAntiviral::ResistancePositionContainer::const_iterator& it_res, 
                                                   const biopp::PseudoNucSequence& sequence,
                                                   double p)
{
    /*!< iteration on aminoacids of the current resistance */
    for (std::string::const_iterator amino = it_res->aminoacids.begin(); amino != it_res->aminoacids.end(); ++amino)
    {   
        /*!< iteration on triplets of the current aminoacid */     
        for (biopp::TripletsIterator it_t = biopp::GeneticCode::triplets(*amino); !it_t.end(); ++it_t)
        {
            /*!< calculate probability */
            const biopp::PseudoTriplet original_triplet = sequence.get_triplet(it_res->pos);
            const size_t distance = original_triplet.distance(biopp::PseudoTriplet(it_t->to_string()));
            p += std::pow(PMUT, distance) * factorial<double>(distance);
        }
    }
}



}